//
//  MyMessageBox.m
//  MyProject_PopMessageDemo
//
//  Created by mac on 15-5-29.
//  Copyright (c) 2015年 zhimeng. All rights reserved.
//

#import "MyMessageBox.h"

@implementation MyMessageBox

+ (void)showMessage:(NSString *)message duration:(CGFloat)seconds
{
    CGFloat W = [UIScreen mainScreen].bounds.size.width;
    CGFloat H = [UIScreen mainScreen].bounds.size.height;
    CGFloat L = 200;// label 定宽
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//先加一个 show_view
    UIView *show_view = [[UIView alloc] initWithFrame:CGRectZero];
    show_view.backgroundColor = [UIColor blackColor];
    show_view.alpha = 1.0f;
    show_view.layer.cornerRadius = 5.0f;
    //showview.layer.masksToBounds = YES;
    
    [window addSubview:show_view];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    //label.numberOfLines=0; 一般不用支持多行
//计算 label 的高度
    CGSize textSize = CGSizeMake(L, 0);
    NSDictionary *attribute = @{ NSFontAttributeName:[UIFont systemFontOfSize:15.0f] };
    CGRect labelRect = [message boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil];
    
    label.frame = CGRectMake(0, 10, L, labelRect.size.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:17.0f];
    
    [show_view addSubview:label];
    
//设置 show_view 的尺寸 根据 label
    show_view.frame = CGRectMake((W-L)/2, H*0.5, L, labelRect.size.height+20);
    
//消失动画
    [UIView animateWithDuration:seconds animations:^{
        show_view.alpha = 0.0f;
    }completion:^(BOOL finished){
        [show_view removeFromSuperview];
    }];
    
}

@end










